package com.zxk28.notebook;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class DataEntryDialog {
	
	// resource
	private static ResourceBundle resourceBundle = ResourceBundle.getBundle("notebook"); 
	
	// shell
	private Shell shell = null;
	
	// other variable
	private String[] labels = null;
	private String[] values = null;
	
	private boolean selectOk = false;
	
	public DataEntryDialog(Shell parent) {
		this.shell = new Shell(parent,SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		this.shell.setLayout(new GridLayout());
	}
	
	public String[] open(){
		createTextWidgets();
		createControlButtons();
		shell.pack();
		shell.open();
		Display display = shell.getDisplay();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		if(selectOk == false)
			values = null;
		return getValues();
	}

	public String[] getValues() {
		return values;
	}
	
	public void setValues(String... values){
		this.values = values;
	}
	
	public void setLabels(String... labels){
		this.labels = labels;
	}

	private void createControlButtons() {
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		
		Button okButton = new Button(composite, SWT.PUSH);
		GridData data = new GridData(80, SWT.DEFAULT);
		okButton.setLayoutData(data);
		okButton.setText(resourceBundle.getString("OK"));
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectOk = true;
				shell.close();
			}
		});
		
		Button cancelButton = new Button(composite, SWT.PUSH);
		cancelButton.setLayoutData(data);
		cancelButton.setText(resourceBundle.getString("Cancel"));
		cancelButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				values = null;
				shell.close();
			}
		});
		
		shell.setDefaultButton(okButton);
		
	}

	private void createTextWidgets() {
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		GridLayout layout = new GridLayout(2, false);
		composite.setLayout(layout);
		
		if(values == null)
			values = new String[labels.length];
		

		// Id
		Label label = new Label(composite, SWT.RIGHT);
		label.setText(labels[0]);
		
		Text text = new Text(composite, SWT.BORDER);
		text.setEnabled(false);
		GridData data = new GridData();
		data.widthHint = 120;
		text.setLayoutData(data);
		text.setData("index", new Integer(0));
		addTextListener(text);
		if(values[0] != null)
			text.setText(values[0]);
		
		// Title
		label = new Label(composite, SWT.RIGHT);
		label.setText(labels[1]);
		
		text = new Text(composite, SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint = 300;
		text.setLayoutData(data);
		text.setData("index", new Integer(1));
		addTextListener(text);
		if(values[1]!= null) 
			text.setText(values[1]);
		
		// content
		label = new Label(composite, SWT.RIGHT);
		label.setText(labels[2]);
		
		text = new Text(composite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.WRAP);
		data = new GridData(GridData.FILL_BOTH);
		data.widthHint = 300;
		data.heightHint = 200;
		text.setLayoutData(data);
		text.setData("index", new Integer(2));
		addTextListener(text);
		if(values[2] != null) 
			text.setText(values[2]);
		
		// date
		label = new Label(composite, SWT.RIGHT);
		label.setText(labels[3]);
		
		text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		data = new GridData();
		data.widthHint = 120;
		text.setLayoutData(data);
		text.setData("index", new Integer(3));
		addTextListener(text);
		text.setText(new SimpleDateFormat(resourceBundle.getString("DatePattern")).format(new Date()));
	}
	
	private void addTextListener(final Text text){
		text.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				int index = (Integer)(text.getData("index"));
				values[index] = text.getText();
			}
		});
	}
}
